package com.join.template.html.node;

import cn.hutool.core.util.StrUtil;
import com.join.template.core.constant.MarkedWords;
import com.join.template.core.element.Element;
import com.join.template.core.element.verify.NodeVerify;
import com.join.template.core.example.AbstractExample;
import com.join.template.core.grammar.info.GrammarInfo;
import lombok.Getter;


/**
 * @author CAOYOU
 * @Title: ListNode
 * @date 2019/10/1815:52
 */
@Getter
public class VarNodeExample extends AbstractExample implements NodeVerify, Element {


    private String var;

    @Override
    public void readAttributes() {
        String replace = this.original.replace(this.expression.getVarTagStart(), "");
        this.var = replace.replaceFirst(this.expression.getVarTagEnd(), "");
        this.attributes.put(this.expression.getAttVar(), this.var);
    }


    @Override
    public void verify() {
    }

    @Override
    public String getExample() {
        StringBuilder grammar = new StringBuilder();
        grammar.append(expression.getVarTagStart());
        grammar.append(MarkedWords.getAttrParamName()).append(" ");
        grammar.append(expression.getVarTagStart());
        return grammar.toString();
    }

    @Override
    public String genExample(GrammarInfo grammarInfo) {
        StringBuilder builder = new StringBuilder();
        builder.append(expression.getVarTagStart());
        if (grammarInfo != null && StrUtil.isNotBlank(grammarInfo.getParentName())) {
            builder.append(grammarInfo.getAlias());
            builder.append(".");
        }
        builder.append(grammarInfo.getName());
        builder.append(expression.getVarTagEnd());
        return builder.toString();
    }


}
